import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/pages/lf_chat_detail/widget/LfMessageMenu.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/service/lf_auth_service.dart';

import '../../../entity/entities/chat_new_message_entity.dart';
import '../../../res/lf_color.dart';
import '../../../res/lf_fonts.dart';
import '../../../res/lf_int_ext.dart';
import '../../../res/lf_text_style.dart';
import 'LfAudioNoise.dart';

class LfMessageAudio extends StatelessWidget {
  final ChatNewMessageEntity data;
  final Function() recall;
  final Function() report;
  final Function() clickAction;
  final bool isCurrent;
  final double currentDuration;
  final PlayerState playerState;
  final bool canShowMenu;
  const LfMessageAudio({
    Key? key,
    required this.data,
    required this.recall,
    required this.report,
    required this.isCurrent,
    required this.currentDuration,
    required this.playerState,
    required this.clickAction,
    required this.canShowMenu,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    LfAuthService authService = Get.find();
    var isSender =
        data.sender?.profile?.userId == authService.currentAccount!.userId;
    var duration = parseInt(data.duration);
    if (duration <= 1) {
      duration = 1;
    }
    var halfWidth = ScreenUtil().screenWidth / 3;
    var width = halfWidth + duration * (halfWidth) / 100;
    var progress = 1.0;
    var icons = Icons.play_circle_outline;
    bool isLoading = false;
    if (isCurrent) {
      progress = currentDuration / (duration.toDouble() * 1000.0);
      if (progress > 1.0) {
        progress = 1.0;
      }
      if (playerState == PlayerState.playing) {
        icons = Icons.pause_circle_outline;
        if (progress <= 0.02) {
          icons = Icons.pause_circle_outline;
          isLoading = true;
        } else {
          isLoading = false;
        }
      } else {
        icons = Icons.play_circle_outline;
        isLoading = false;
      }
    }
    Widget child = isSender
        ? Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Flexible(
                  child: getMenuWidget(
                      data: data,
                      child: InkWell(
                        child: Container(
                          decoration: BoxDecoration(
                              color: color514F62,
                              borderRadius: BorderRadius.only(
                                  topLeft: Radius.circular(10.r),
                                  bottomRight: Radius.circular(10.r),
                                  bottomLeft: Radius.circular(10.r))),
                          padding: EdgeInsets.symmetric(
                              horizontal: 10.r, vertical: 8.h),
                          constraints: BoxConstraints(minHeight: 42.h),
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              LfAudioNoise(
                                width: width,
                                isCurrent: isCurrent,
                                progress: progress,
                                startColor: colorFEFEFE,
                                endColor: Colors.white38,
                              ),
                              5.w.sbw,
                              Text(
                                '$duration"',
                                textWidthBasis: TextWidthBasis.longestLine,
                                style: lfTextStyle(
                                    sp: 16.sp,
                                    fontFamily: fontRegular,
                                    color: colorFEFEFE),
                              ),
                              5.w.sbw,
                              Container(
                                width: 20.r,
                                height: 20.r,
                                alignment: Alignment.center,
                                child: isLoading
                                    ? SizedBox(
                                        width: 16.r,
                                        height: 16.r,
                                        child: CircularProgressIndicator(
                                          color: colorFEFEFE,
                                          strokeWidth: 2.r,
                                        ),
                                      )
                                    : Icon(
                                        icons,
                                        size: 20.r,
                                        color: colorFEFEFE,
                                      ),
                              )
                            ],
                          ),
                        ),
                        onTap: () {
                          clickAction.call();
                        },
                      ),
                      isSender: isSender,
                      recall: recall,
                      report: report,
                      canShowMenu: canShowMenu,
                      reply: () {},
                      isTextMessage: false)),
              10.w.sbw,
            ],
          )
        : Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              10.w.sbw,
              // CustomCacheImage(
              //   imageUrl: data.sender?.profile?.avatarUrl,
              //   isCircle: true,
              //   width: 40.r,
              //   height: 40.r,
              // ),
              // 5.w.sbw,
              Flexible(
                  child: InkWell(
                child: Container(
                  decoration: BoxDecoration(
                      color: color8F8F8F,
                      borderRadius: BorderRadius.only(
                          topRight: Radius.circular(10.r),
                          bottomRight: Radius.circular(10.r),
                          bottomLeft: Radius.circular(10.r))),
                  padding:
                      EdgeInsets.symmetric(horizontal: 10.r, vertical: 8.h),
                  constraints: BoxConstraints(minHeight: 42.h),
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Container(
                        width: 16.r,
                        height: 16.r,
                        alignment: Alignment.center,
                        child: isLoading
                            ? SizedBox(
                                width: 12.r,
                                height: 12.r,
                                child: CircularProgressIndicator(
                                  color: colorFEFEFE,
                                  strokeWidth: 2.r,
                                ),
                              )
                            : Icon(
                                icons,
                                size: 16.r,
                                color: colorFEFEFE,
                              ),
                      ),
                      5.w.sbw,
                      LfAudioNoise(
                        width: width,
                        isCurrent: isCurrent,
                        progress: progress,
                        startColor: colorFEFEFE,
                        endColor: Colors.grey,
                      ),
                      5.w.sbw,
                      Text(
                        '$duration"',
                        textWidthBasis: TextWidthBasis.longestLine,
                        style: lfTextStyle(
                            sp: 16.sp,
                            fontFamily: fontRegular,
                            color: colorFEFEFE),
                      ),
                    ],
                  ),
                ),
                onTap: () {
                  clickAction.call();
                },
              )),
              5.w.sbw,
            ],
          );

    return child;
  }
}
